#!/usr/bin/env python
# stoker_ping: Implementation of Stoker "ping" command
#
# Author:   Mike Murphy (mamurph@cs.clemson.edu)
# Revision: 26 February 2009
#
#   Copyright 2007-2009 Clemson University
#
#   Licensed under the Apache License, Version 2.0 (the "License");
#   you may not use this file except in compliance with the License.
#   You may obtain a copy of the License at
#
#       http://www.apache.org/licenses/LICENSE-2.0
#
#   Unless required by applicable law or agreed to in writing, software
#   distributed under the License is distributed on an "AS IS" BASIS,
#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#   See the License for the specific language governing permissions and
#   limitations under the License.
#
# This material is based upon work supported under a National Science
# Foundation Graduate Research Fellowship. Any opinions, findings,
# conclusions or recommendations expressed in this publication are those of
# the author(s) and do not necessarily reflect the views of the National
# Science Foundation.

import stoker_base

class PingCommand(stoker_base.CommandObjectBase):
   def __init__(self, stoker_config, opts, args, target, extra):
      stoker_base.CommandObjectBase.__init__(self, stoker_config, opts, args, \
                                             target, extra)
      # Prepend any non-special opts to the args
      count = '1'
      timeout = '10'
      idx = 0
      for opt, val in opts:
         if opt == '-c':
            count = val
         elif opt == '-W':
            timeout = val
         else:
            self.args.insert(idx, opt)
            idx += 1
            if val != '':
               self.args.insert(idx, val)
               idx += 1
      # Build the ping command and arguments
      self.command = 'ping'
      out_args = [ '-c', count, '-W', timeout ]
      for arg in self.args:
         out_args.append(arg.strip())
      out_args.append(target)
      self.args = out_args
   #
   def execute(self, stdin=''):
      return stoker_base.run_external(self.command, self.args)
   #
